<?php

namespace frontend\controllers;

/**
 * @author  shuang
 * @date    2016-10-26 13:39:38
 * @version V1.0
 * @desc    
 */
use common\components\WController;
use Yii;
use frontend\models\WB_UserProfile;
use yii\helpers\Json;
use common\models\User;
use yii\helpers\ArrayHelper;

class ProfileController extends WController {
    /*
     * 账户详情
     */

    public function actionInfo() {
        $userid = Yii::$app->user->id;
        $res = WB_UserProfile::getInfo($userid);
        if (!$res) {
            throw new \yii\web\NotFoundHttpException();
        }
        return $this->render("info", ["res" => $res]);
    }

    /*
     * 代理详情                
     */

    public function actionUserbind() {
        $res = WB_UserProfile::getAgentDetail(Yii::$app->user->identity->my_code);
        return $this->render("userbind", $res);
    }

    /*
     * 修改密码
     */

    public function actionUpdatepwd() {
        if (Yii::$app->request->isAjax) {
            $model = User::findOne(Yii::$app->user->id);
            if (Yii::$app->getSecurity()->validatePassword(Yii::$app->request->post("oldpassword"), $model->password_hash)) {
                $model->setPassword(Yii::$app->request->post("password"));
                $model->removePasswordResetToken();
                if ($model->save(false)) {
                    echo Json::encode(["status" => true, "msg" => "密码修改成功！", "url" => Yii::$app->getHomeUrl()]);
                } else {
                    echo Json::encode(["status" => false, "msg" => "密码修改失败！"]);
                }
            } else {
                echo Json::encode(["status" => false, "msg" => "原密码不正确！"]);
            }
            exit;
        }
        return $this->render("updatepwd");
    }

}
